'use strict';
var fs = require('fs');
var path = require('path');

function scanDir(startPath) {
    var result = [];
    function finder(scanPath) {
        var files = fs.readdirSync(scanPath);
        for(var i=0;i<files.length;i++){
            var filePath=path.join(scanPath,files[i]);
            var stats=fs.statSync(filePath);
            if(stats.isFile()){
                result.push(filePath);
            }else{
                finder(filePath);
            }
        }
    }
    finder(startPath);
    return result;
}

//自动扫描API目录并加载所有api
var api = function (app, router, jwt) {
    var result = scanDir(path.join(__dirname,'api'));
    for(var i=0;i<result.length;i++) {
        require(result[i])(app, router, jwt);
    }
    return router;
};

module.exports = api;
